﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;

namespace HelloASPX
{
    public partial class register : System.Web.UI.Page
    {
        protected void registerButton_Click(object sender, EventArgs e)
        {
            try
            {
                if(txtUsername.Text == "" || txtMima.Text == "" || txtAnswer.Text == "")
                {
                    return;
                }
                string sex = sexBList.SelectedValue;
                //string photopath = Session["photo"].ToString();

                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/App_Data/user.xml"));
                Console.Write(doc);
                XmlElement usernode = doc.CreateElement("user");

                XmlElement namenode = doc.CreateElement("username");
                namenode.InnerText = txtUsername.Text;
                usernode.AppendChild(namenode);

                XmlElement mimanode = doc.CreateElement("mima");
                mimanode.InnerText = txtMima.Text;
                usernode.AppendChild(mimanode);

                XmlElement qunode = doc.CreateElement("passwordQuestion");
                qunode.InnerText = ddlQu.SelectedValue;
                usernode.AppendChild(qunode);

                XmlElement answernode = doc.CreateElement("answer");
                answernode.InnerText = txtAnswer.Text;
                usernode.AppendChild(answernode);

                XmlElement mailnode = doc.CreateElement("e-mail");
                mailnode.InnerText = txtEmail.Text;
                usernode.AppendChild(mailnode);

                XmlElement sexnode = doc.CreateElement("sex");
                sexnode.InnerText = sex;
                usernode.AppendChild(sexnode);

                XmlElement agenode = doc.CreateElement("age");
                agenode.InnerText = txtAge.Text;
                usernode.AppendChild(agenode);

                XmlElement csrqnode = doc.CreateElement("birthdate");
                csrqnode.InnerText = txtcsrq.Text;
                usernode.AppendChild(csrqnode);

                XmlElement telnode = doc.CreateElement("tel");
                telnode.InnerText = txtTel.Text;
                usernode.AppendChild(telnode); 

                XmlElement photonode = doc.CreateElement("photo");
                string photopath = "~/img/" + photo.FileName;
                photonode.InnerText = photopath;
                usernode.AppendChild(photonode);

                doc.DocumentElement.AppendChild(usernode);
                doc.Save(Server.MapPath("~/App_Data/user.xml"));

                Response.Write("<script>alert('注册成功');location='Login.aspx'</script>");
            }
            catch
            {
                Response.Write("<script>alert('注册失败');</script>");
            }
        }

        protected void txtUsername_TextChanged(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/user.xml"));
            int flag = 1;
            XmlNodeList usernodes = doc.SelectNodes("//user[username='" + txtUsername.Text + "']");
            if (usernodes.Count > 0)
            {
                userNameLabel.Text = "该用户名已被占用，请换一个用户名吧！";
                flag = 0;
            }
            else
            {
                userNameLabel.Text = "该用户名可用。";
            }
            Regex reg = new Regex(@"\w{3,50}");
            if (reg.IsMatch(txtUsername.Text)&&flag==1)
            {
                userNameLabel.Text = "该用户名可用。";
            }
            else if(flag == 1)
            {
                userNameLabel.Text = "该用户名不可用，请输入一个3~50字符的用户名！";
            }
        }

        protected void txtMima_TextChanged(object sender, EventArgs e)
        {
            string text = txtMima.Text;
            Regex reg = new Regex(@"[A-Za-z0-9]{6,12}");
            if (reg.IsMatch(text))
            {
                mimaLabel.Text = "该密码可用。";
            }
            else
            {
                mimaLabel.Text = "该密码不可用，请将密码设置为6~12个字符之间！";
            }
        }

        protected void txtEmail_TextChanged(object sender, EventArgs e)
        {
            string email = txtEmail.Text;
            Regex reg = new Regex(@"[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)");
            if (reg.IsMatch(email))
            {
                txtEmailLabel.Text = "该邮箱可用。";
            }
            else
            {
                //"这可不是一个邮箱啊，仔细检查一下有没有写错吧！"
                txtEmailLabel.Text = txtEmail.Text + reg.IsMatch(email);
            }
        }
        protected void txtAge_TextChanged(object sender, EventArgs e)
        {

            if (txtAge.Text == null)
            {
                txtAge.Text = 0+"";
            }

            int age = Int32.Parse(txtAge.Text);

            if (age >= 0 && age <= 120)
            {
                ageLabel.Text = "原来你"+ txtAge.Text +"岁啊。";
                txtcsrq.Text = new DateTime(Int32.Parse(DateTime.Now.ToString("yyyy")) - Int32.Parse(txtAge.Text), 1, 1).ToString("yyyy-MM-dd");
            }
            else
            {
                ageLabel.Text = "等下，你怎么可能" + txtAge.Text + "岁啊！";
            }
        }

        protected void txtcsrq_TextChanged(object sender, EventArgs e)
        {

            if (txtAge.Text == null)
            {
                txtAge.Text = 0+"";
            }

            string today = DateTime.Now.ToString("yyyy");
            int todaynum = Int32.Parse(today);

            string [] a = txtcsrq.Text.Split('-');

            int birthday = Int32.Parse(a[0]);
            int btn = todaynum - birthday;

            int age = Int32.Parse(txtAge.Text);
            if (btn == age)
            {
                csrqLabel.Text = "嗯嗯，看起来没错，你是"+ txtcsrq.Text + "来这里的。";
            }
            else
            {
                csrqLabel.Text = "不对啊，照这么算的话，你才"+ btn + "岁！和你的年龄不一致哦！";
                txtAge.Text = btn + "";
                ageLabel.Text = "原来你" + txtAge.Text + "岁啊。";
            }
        }

        protected void txtTel_TextChanged(object sender, EventArgs e)
        {
            /*Regex reg = new Regex(@"(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}");
            string tel = txtTel.Text;
            if (reg.IsMatch(tel))
            {
                txtTelLabel.Text = txtTel.Text + "可不是一个有效的电话号码哦！";
            }
            else
            {
                txtTelLabel.Text = "该电话号码可用。";
            }*/
        }

        protected void sexBList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void confirmPassword_TextChanged(object sender, EventArgs e)
        {
            if(txtMima.Text != confirmPassword.Text)
            {
                confirmPasswordLabel.Text = "两次输入的密码不一致！";
            }
            else
            {
                confirmPasswordLabel.Text = "";
            }
        }
    }
}